<!-- 客服排班信息 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="客服排班列表" name="one">
        <SearchTable :platformList="platformList" :rentMobileOrderType="rentMobileOrderType" ref="searchTableRef" v-model:formStatus="formStatus" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="客服排班详情" name="two">
        <FormTab :platformList="platformList" :rentMobileOrderType="rentMobileOrderType" :queryDate="queryDate" v-model:formStatus="formStatus" v-model:form="form" @handleInitData="clearData" @handleSuccess="handleSuccess" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="customerSchedule">
import SearchTable from "@/components/customerSchedule/customerSchedule/SearchTable.vue";
import FormTab from "@/components/customerSchedule/customerSchedule/FormTab.vue";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { TabsPaneContext } from "element-plus";
import { useDictData } from "@/hooks/network/useDictData";
import type { IDictData } from "@/types/dict";
import type { ICustomerScheduleDTO, ICustomerScheduleList } from "@/types/customerSchedule/customerSchedule";
let { getDictData } = useDictData();
/* 平台类型 */
const platformList = ref<Array<IDictData>>([]);
/* 租机类型 */
const rentMobileOrderType = ref<Array<IDictData>>([]);

/* 表格状态 */
const formStatus = ref<ViewMode>("none");
/* 标签页切换 */
const activeName = ref<TabsMode>("one");
/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");

const form = reactive<ICustomerScheduleDTO>({
  id: 0,
  platform: [],
  type: [],
  start_time: 0,
  end_time: 0,
  master_id_list: []
});

const queryDate = ref<Array<string>>([]);
/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}

/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}
/* 点击编辑按钮 */
function handleViewEdit(row: ICustomerScheduleList, code: ViewModeFilter) {
  formStatus.value = code;
  activeName.value = "two";
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    form.id = row.id;
    form.platform = row.platform.map(item => item.platform);
    form.type = row.type.map(item => item.type);
    form.start_time = row.start_time * 1000;
    form.end_time = row.end_time * 1000;
    console.log(form.start_time);
    queryDate.value = [row.start_time + "", row.end_time + ""];
    form.master_id_list = row.customerMaster.map(item => item.master_id);
  });
}

getDictData("PLATFORM_TYPE", platformList);
getDictData("RENT_MOBILE_ORDER_TYPE", rentMobileOrderType);
</script>

<style lang="scss" scoped></style>
